package com.taxi.driver.module.order.pay;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taxi.driver.R;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.BizStatus;
import com.taxi.driver.config.DriverType;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.config.PositionType;
import com.taxi.driver.data.entity.WxpayInfo;
import com.taxi.driver.module.order.pay.dagger.DaggerOrderPayComponent;
import com.taxi.driver.module.order.pay.dagger.OrderPayModule;
import com.taxi.driver.module.pay.AlipayUtils;
import com.taxi.driver.module.pay.WxPayUtils;
import com.taxi.driver.socket.SocketEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述：
 */
public class OrderPayActivity extends BaseActivity implements OrderPayContract.View {

    private String mOrderUuid;

    public static void actionStart(Context context, String orderUuid, double totalPrice) {
        Intent intent = new Intent(context, OrderPayActivity.class);
        intent.putExtra(IConstants.ORDER_UUID, orderUuid);
        intent.putExtra(IConstants.PRICE, totalPrice);
        context.startActivity(intent);
    }

    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.tv_hint)
    TextView mHint;
    @BindView(R.id.layout_wechat)
    LinearLayout mLayoutWechat;
    @BindView(R.id.view_weipay)
    View mViewWeipay;

    @Inject
    OrderPayPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_pay);
        ButterKnife.bind(this);
        DaggerOrderPayComponent.builder().appComponent(getAppComponent())
                .orderPayModule(new OrderPayModule(this)).build().inject(this);

        mPresenter.onCreate();
        mOrderUuid = getIntent().getStringExtra(IConstants.ORDER_UUID);
        mPresenter.setOrderUuid(mOrderUuid);
        showPriceInfo(getIntent().getDoubleExtra(IConstants.PRICE, 0));
        if (mPresenter.driverType() == DriverType.SPECIAL) {
            mHint.setText(getResources().getString(R.string.order_pay_notice));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.unsubscribe();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void showPriceInfo(double price) {
        mTvPrice.setText(String.format(Locale.CHINA, "%.01f", price) + "元");
    }

    @Override
    public void paySuccess() {
        toast("支付成功");
        // 发送司机位置（司机支付）
        EventBus.getDefault().post(new SocketEvent(SocketEvent.SEND_POSITION, BizStatus.EMPTY_DRIVING, PositionType.SJZF, mOrderUuid));
        finish(); //关闭界面
    }

    @Override
    public void payFail() {
        toast("支付失败");
    }

    @Override
    public void startAlipay(String tradeUrl) {
        AlipayUtils.getInstance(this).pay(tradeUrl); //执行支付宝支付
    }

    @Override
    public void startWxpay(WxpayInfo info) {
        WxPayUtils.getInstance(this).pay(info); //执行微信支付
    }

    @Override
    public void close() {
        finish();
    }

    /**
     * 获取本地ip地址
     */
    private String getIp() {
        WifiManager wifiManager =  (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        if (ipAddress == 0) return "127.0.0.1";
        return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
                + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
    }

    @OnClick({R.id.layout_wechat, R.id.layout_alipay, R.id.layout_balance, R.id.iv_close, R.id.view_bg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_wechat:
                mPresenter.reqPayByWeixin(getIp());
                break;
            case R.id.layout_alipay:
                mPresenter.reqPayByAlipay();
                break;
            case R.id.layout_balance:
                mPresenter.reqPayByBalance();
                break;
            case R.id.iv_close:
            case R.id.view_bg:
                finish(); //关闭界面
                break;
        }
    }
}
